﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using IFPRJ.EF.DALayer;
using IFPRJ.EF.DALayer.Providers.Sqlserver;
using IFPRJ.EF.Model;
using IFPRJ.EF.Components;
using System.Web;

namespace IFPRJ.Areas.Admin
{
    public class PostController:AdminBaseController
    {
        IPost postprovider = new PostProvider();
        IPostCatory provider = new PostCatoryProvider();
        private SelectList GetDropDownList(int? id)
        {
            List<SelectListItem> items = new List<SelectListItem>();
            var post = provider.PostCatoryLoadAll();
            return  new SelectList(post, "Id", "Name",id.ToString());
        }
        public ActionResult Index()
        {
            var postlist = postprovider.PostLoadAll();
            this.ViewData["postlist"] = postlist;
            return View("Index",new List<Post>(postlist));
        }

        public ActionResult Create()
        {
            this.ViewData["postcatorylist"] = GetDropDownList(null);
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(Post _post, string postcatorylist, string FckTextBox)
        {
            int catoryid = Convert.ToInt32(postcatorylist);
            _post.Context = FckTextBox;
            _post.Date_Post = DateTime.Now;
            _post.Post_CatoryID = catoryid;
            postprovider.PostCreate(_post);
            return RedirectToAction("Index");
        }


        public ActionResult Edit(int id)
        {
            Post post = postprovider.PostGetSingleByPostId(id);
            this.ViewData["postcatorylist"] = GetDropDownList(post.Post_CatoryID);
            return View(post);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        [HttpPost, ActionName("Edit")]
        public ActionResult EditSave(int id,Post _post, string postcatorylist, string FckTextBox)
        {
            _post.ID = id;
            _post.Context = FckTextBox;
            _post.Date_Lastupdated = DateTime.Now;
            _post.Post_CatoryID = Convert.ToInt32(postcatorylist);
            if (ModelState.IsValid)
            {
                postprovider.PostUpdate(_post);
                return RedirectToAction("Index");
            }
            return RedirectToAction("Edit");
        }



        public ActionResult Delete(int id)
        {
            Post deletepost = postprovider.PostGetSingleByPostId(id);
            return View(deletepost);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Post deletepost = postprovider.PostGetSingleByPostId(id);
            postprovider.PostDelete(deletepost);

            return RedirectToAction("Index");
        }

    }
}
